home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 October / Chip Ekim 2003.iso / prog / code / contr / setup.exe / Disk1 / data1.cab / Configuration_En / Commands / ShortcutClass.js < prev    next >
Encoding:
JavaScript  |  2003-07-18  |  8.9 KB  |  334 lines

  1. // Copyright 2000, 2001, 2002, 2003 Macromedia, Inc. All rights reserved.
  2.  
  3. /* ----------------------------------------------------------------------*/
  4. //Shortcut List Class
  5.  
  6. // This class represents an xml file used to store a set of shortcut keys.
  7. // This class uses the file class
  8. // The paths are represented by URL's for cross platform compatibility.
  9. // example path = file:///C|/Program Files/Dreamweaver 3/Configuration/Menus/Custom Sets/test set.xml
  10.  
  11. //INTERFACE
  12. //
  13. //Shortcut List methods:
  14. //  readFromShortcutXML(theURL)
  15. //  writeToShortcutXML(theURL)
  16. //  getKeyByID(id)
  17. //  writeToMenusXML
  18. //  locateById
  19. //  changeById
  20. //  addShortcut
  21. //  deleteShortcut
  22.  
  23.  
  24. //Constructor function
  25. //
  26. function ShortcutList()
  27. {
  28.   // properties
  29.   this.shortcutListName = "";
  30.   this.idKeyPairList = new Array(); // Array of objects with two properties = .id and .keysArray
  31.   this.changeList = new Object();
  32.  
  33.   // initialization
  34.   // this.readFromShortcutXM(theURL);
  35. }
  36.  
  37. // static properties
  38. ShortcutList.SHORTCUTLIST_TAG = 'SHORTCUTSET';
  39. ShortcutList.SHORTCUT_TAG = 'SHORTCUT';
  40.  
  41. // static methods
  42.  
  43.  
  44. // methods
  45. ShortcutList.prototype.readFromShortcutXML = ShortcutList_readFromShortcutXML;
  46. ShortcutList.prototype.writeToShortcutXML = ShortcutList_writeToShortcutXML;
  47. ShortcutList.prototype.getKeyByID = ShortcutList_getKeyByID;
  48. ShortcutList.prototype.getIdByKey = ShortcutList_getIdByKey;
  49. ShortcutList.prototype.addShortcut = ShortcutList_addShortcut;
  50. //ShortcutList.prototype.compare = ShortcutList_compare;
  51. ShortcutList.prototype.getIndexByID = ShortcutList_getIndexByID;
  52. ShortcutList.prototype.del = ShortcutList_del;
  53. ShortcutList.prototype.update = ShortcutList_update;
  54. ShortcutList.prototype.compareUpdate = ShortcutList_compareUpdate;
  55. ShortcutList.prototype.clearShortcutList = ShortcutList_clearShortcutList;
  56. ShortcutList.prototype.length = ShortcutList_length;
  57. ShortcutList.prototype.changeListLength = ShortcutList_changeListLength;
  58.  
  59. // reads in the data from a shortcuts.xml file to generate a shortcut list.
  60. function ShortcutList_readFromShortcutXML(theURL)
  61. {
  62.   var shortcutDOM, theShortcutSet,i, shortcutFile ;
  63.   shortcutDOM = dreamweaver.getDocumentDOM(theURL);
  64.   theShortcutSet = shortcutDOM.getElementsByTagName(ShortcutList.SHORTCUTLIST_TAG);
  65.   this.clearShortcutList();
  66.   this.shortcutListName = shortcutDOM.getElementsByTagName('SHORTCUTSET')[0].getAttribute('name');
  67.   theShortcuts = theShortcutSet[0].getElementsByTagName(ShortcutList.SHORTCUT_TAG);
  68.   for (i=0;i<theShortcuts.length;i++)
  69.   {
  70.     this.addShortcut(theShortcuts[i].getAttribute("ID"), theShortcuts[i].getAttribute("keys"));
  71.   }
  72. }
  73.  
  74. // writes out data to shortcuts.xml file from shortcut list (blows away old file). Returns true if successfull
  75. function ShortcutList_writeToShortcutXML(theURL,listName)
  76. {
  77.   var theContents = new Array(), tempStr = "", retVal;
  78.   var shortcutSetFile, i;
  79.   if (getExtension(theURL) !="xml")
  80.     theURL += ".xml";
  81.   this.shortcutListName = listName;
  82.   theContents.push('<' + ShortcutList.SHORTCUTLIST_TAG + ' name="' + this.shortcutListName + '">\n');
  83.   for(i=0;i<this.idKeyPairList.length;i++)
  84.   {
  85.     tempStr = '  ' + '<' + ShortcutList.SHORTCUT_TAG + ' ID="' + this.idKeyPairList[i].id;
  86.     tempStr += '"  keys="' + this.idKeyPairList[i].keysArray.join(",") + '"/>\n';
  87.     theContents.push(tempStr);
  88.   }
  89.   theContents.push('</' +ShortcutList.SHORTCUTLIST_TAG + '>' );
  90.   shortcutSetFile = new File(theURL);
  91.   retVal = shortcutSetFile.setContents(theContents.join(""));
  92.     return retVal;
  93. }
  94.  
  95. // Returns the key(s) for a given id. If the id doesn't exist, returns ""
  96. function ShortcutList_getKeyByID(id)
  97. {
  98.   var returnKey="", i;
  99.   for(i=0;i<this.idKeyPairList.length;i++)
  100.   {
  101.     if (this.idKeyPairList[i].id == id)
  102.     {
  103.       returnKey = this.idKeyPairList[i].keysArray.join(",");
  104.       break
  105.     }
  106.   }
  107.   return returnKey;
  108. }
  109.  
  110.  
  111. function ShortcutList_addShortcut(id, keyStr)
  112. {
  113.   var tempObj = new Object();
  114.   tempObj.id = id;
  115.   tempObj.keysArray = (keyStr) ? keyStr.split(",") : new Array();
  116.   this.idKeyPairList.push(tempObj);
  117. }
  118.  
  119. // takes in two ShortcutLists and compares them. Returns true if
  120. /*function ShortcutList_compare(shortcutListA)
  121. {
  122.   var retVal = true, i;
  123.   if (this.idKeyPairList.length == shortcutListA.idList.length)
  124.   {
  125.     for (i=0;(i<this.idKeyPairList.length) && (retVal==true); i++)
  126.     {
  127.       index = shortcutListA.getIndexByID(this.idList[i])
  128.       if (this.idKeyPairList[i].keys != shortcutListA.idKeyPairList[index].keys)
  129.         retVal = false;
  130.     }
  131.   }
  132.   else
  133.     retVal = false;
  134.   return retVal;
  135. }
  136. */
  137. function idOrder (a,b)
  138. {
  139.   if (a.id == b.id)
  140.     return 0;
  141.   else if (a.id > b.id)
  142.     return 1;
  143.   else
  144.     return -1;
  145. }
  146.  
  147. function ShortcutList_compare(shortcutListA)
  148. {
  149.   var retVal = true;
  150.   if (shortcutListA.idKeyPairList.length == this.idKeyPairList.length)
  151.   {
  152.     shortcutListA.idKeyPairList.sort(idOrder);
  153.     this.idKeyPairList.sort(idOrder);
  154.     for (var i=0; i<this.idKeyPairList.length; i++)
  155.     {
  156.       if (shortcutListA.idKeyPairList[i].id != this.idKeyPairList[i].id)
  157.       {
  158.         retVal=false;
  159.         break;
  160.       }
  161.     }
  162.   }
  163.   else
  164.     retVal = false;
  165.   return retVal;
  166. }
  167.  
  168. function ShortcutList_getIndexByID(id)
  169. {
  170.   var i, found = false;
  171.   for (i=0;(i < this.idKeyPairList.length) && !found; i++)
  172.   {
  173.     if (this.idKeyPairList[i].id == id)
  174.     {
  175.       found=true;
  176.       break;
  177.     }
  178.   }
  179.   return (found)? (i):-1;
  180. }
  181.  
  182. // returns the number of changes made to the shortcut list
  183. function ShortcutList_update(id, newKey, oldKey)
  184. {
  185.   var i, retVal=0, indexOldKey, idFound=false, theId;
  186.   for (i=0;i<this.idKeyPairList.length;i++)
  187.   {
  188.     theId = this.idKeyPairList[i].id;
  189.     if (theId == id || focusRelatedId(theId,id))
  190.     {
  191.       indexOldKey = getKeyIndex(this.idKeyPairList[i].keysArray, oldKey);
  192.       if (indexOldKey == -1 && PLATFORM == "Win32")
  193.       {
  194.         if (oldKey.indexOf("Cmd") != -1)
  195.            oldKey = oldKey.replace(/Cmd/,"Ctrl");       
  196.         indexOldKey = getKeyIndex(this.idKeyPairList[i].keysArray, oldKey);
  197.       }
  198.       if (theId == id)
  199.     idFound=true;
  200.       if (indexOldKey == -1 && newKey)
  201.     this.idKeyPairList[i].keysArray.push(newKey);
  202.       else if (newKey && indexOldKey != -1)
  203.     this.idKeyPairList[i].keysArray[indexOldKey] = newKey;
  204.       else if (indexOldKey != -1)
  205.     this.idKeyPairList[i].keysArray.splice(indexOldKey,1);
  206.     
  207.       this.changeList[theId] = true;
  208.       retVal++;
  209.     }
  210.   }
  211.   if (!idFound && newKey)
  212.   {
  213.     this.addShortcut(id, newKey);
  214.     retVal++;
  215.   }
  216.   return retVal;
  217. }
  218.  
  219.  
  220. function ShortcutList_del(id)
  221. {
  222.   var index;
  223.   index = this.getIndexByID(id);
  224.   this.idKeyPairList.splice(index,1);
  225. }
  226.  
  227. function ShortcutList_clearShortcutList()
  228. {
  229.   this.shortcutListName ="";
  230.   this.idKeyPairList = new Array();
  231. }
  232.  
  233. function ShortcutList_getIdByKey(theKey)
  234. {
  235.   var i,x, retVal="", found = false;
  236.   for (i=0;i<this.idKeyPairList.length && !found;i++)
  237.   {
  238.     if (this.idKeyPairList[i].keysArray[0])
  239.     {
  240.       for (x=0; x<this.idKeyPairList[i].keysArray.length; x++)
  241.       {
  242.         if (this.idKeyPairList[i].keysArray[x] == theKey)
  243.         {
  244.           retVal = this.idKeyPairList[i].id;
  245.           found = true;
  246.           break;
  247.         }
  248.       }
  249.     }
  250.   }
  251.   return retVal;
  252. }
  253.  
  254. function ShortcutList_compareUpdate(shortcutListA){
  255.   var index, newKeys, oldKeys;
  256.   this.shortcutListName = shortcutListA.shortcutListName
  257.   for (var i=0; i<shortcutListA.length(); i++){
  258.     index = this.getIndexByID(shortcutListA.idKeyPairList[i].id);
  259.     if (index != -1){
  260.       oldKeys = this.idKeyPairList[index].keysArray;
  261.       newKeys = shortcutListA.idKeyPairList[i].keysArray
  262.       if (oldKeys.toString() != newKeys.toString()){
  263.         this.idKeyPairList[index].keysArray = shortcutListA.idKeyPairList[i].keysArray;
  264.         this.changeList[shortcutListA.idKeyPairList[i].id] = true;
  265.       }
  266.     }
  267.   }
  268. }
  269.  
  270.  
  271. function getExtension(fileName)
  272. {
  273.   var retVal = "", index;
  274.  
  275.   index = fileName.lastIndexOf(".");
  276.   if (index != -1)
  277.     retVal = fileName.substring(index+1);
  278.   return retVal;
  279. }
  280.  
  281. function getSimpleName(fileName)
  282. {
  283.   var index; retVal = fileName;
  284.  
  285.   index = fileName.lastIndexOf(".");
  286.   if (index != -1)
  287.     retVal = fileName.substring(0,index);
  288.   return retVal;
  289. }
  290.  
  291. function focusRelatedId(idOne,idTwo)
  292. {
  293.   var indexOne, indexTwo;
  294.   if (idOne && idTwo)
  295.   {
  296.     indexOne = idOne.lastIndexOf("_");
  297.     indexTwo = idTwo.lastIndexOf("_");
  298.     return (idOne.substring(indexOne) == idTwo.substring(indexTwo));
  299.   }
  300.   else
  301.     return false;
  302. }
  303.  
  304. // returns -1 if the key is not in the array
  305. function getKeyIndex(theKeyArray, theKey)
  306. {
  307.   var i, retVal=-1;
  308.   if (theKeyArray)
  309.   {
  310.     for (i=0;i<theKeyArray.length;i++)
  311.     {
  312.       if (theKeyArray[i]==theKey)
  313.       {
  314.         retVal=i;
  315.         break;
  316.       }
  317.     }
  318.   }
  319.   return retVal;
  320. }
  321.  
  322. function ShortcutList_length()
  323. {
  324.   return this.idKeyPairList.length
  325. }
  326.  
  327. function ShortcutList_changeListLength()
  328. {
  329.   var retVal = 0;
  330.   for(var i in this.changeList)
  331.     retVal++;
  332.   return retVal;
  333. }
  334.